package de.karlNet.gifSocket.controllers;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.karlNet.gifSocket.SessionStorage;
import de.karlNet.gifSocket.controllers.interfaces.ISession;
import de.karlNet.gifSocket.controllers.interfaces.ISessionController;

@Controller
@RequestMapping("session")
public class SessionController implements ISessionController {
	@Autowired
	private SessionStorage sessionStorage;

	public void joinSession(int sessionNumber, OutputStream client) {
		ISession sessionToJoin = this.sessionStorage.getSession(sessionNumber);
		sessionToJoin.addClient(client);
	}

	public void addPictureToSession(int sessionNumber, BufferedImage imageToAdd) {
		ISession sessionToJoin = this.sessionStorage.getSession(sessionNumber);
		sessionToJoin.addScreenshot(imageToAdd);
	}

	@RequestMapping(value="/create", method=RequestMethod.GET)
	public @ResponseBody String createSession(int x, int y) {
		Rectangle screenSize = new Rectangle(x, y);
		return "" + sessionStorage.createNewSession(screenSize).getSessionNumber();
	}

	public void addMouseListener(int sessionID, OutputStream outputStream) {
		this.sessionStorage.getSession(sessionID).setMouseOutputStream(
				outputStream);
	}

}
